import axios from 'axios';
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  //window.localStorage.getItem("accessToken") 获取token的value
  
  let token = typeof window !== 'undefined' &&  window.localStorage.getItem("token");
  console.log('token',token)
  if (token) {
      //将token放到请求头发送给服务器,将tokenkey放在请求头中
      config.headers.Authorization = token;     
      //也可以这种写法
      // config.headers['accessToken'] = Token;
       return config;
  }
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

export const request = async (url,params)=>{
    return new Promise((resolve,reject)=>{
        axios({
            method: 'post',
            url: url,
            data: params
          })
          .then((res) => {
            // debugger
            //登录过期跳转登录
            if(res.data.success){
                resolve( res.data );
            } else {
                resolve(null)
            }
            
          })
          .catch((error) => {
            reject( error );
          });
    })
}